{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "85d1a3c9-66e0-459f-843d-a9667f73e698",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import Lasso"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c67978-e285-473f-af9b-a14a4c64d5e2",
   "metadata": {},
   "source": [
    "# Lasso回归"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29ff24dc-72f4-409c-8201-a72aad75b324",
   "metadata": {},
   "source": [
    "## 一、Lasso回归的原理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13101be2-8d1b-4d45-81fe-2aacaa8423b2",
   "metadata": {},
   "source": [
    "Lasso回归的核心在于通过对系数进行压缩，以达到变量选择和复杂度调整的目的，从而提高模型的预测精度和解释能力。Lasso回归在处理具有多重共线性数据或高维数据时尤其有效。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1903ec6-684d-4508-80eb-a4296bb3de38",
   "metadata": {},
   "source": [
    "数学表达式：\n",
    "\n",
    "$$\n",
    "J(\\beta) = \\frac{1}{2n} \\sum_{i=1}^n (y_i - x_i^T\\beta)^2 + \\lambda \\left \\| \\beta \\right \\|_1\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af300889-90e6-4e00-a7f4-c5df2fe113d3",
   "metadata": {},
   "source": [
    "Lasso的目标函数包括数据拟合和惩罚项，其中惩罚项系数是L1范数，这使得部分系数严格收缩到零，从而实现自动的特征选择。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710199e1-e492-42af-866b-d8f7f6926cf1",
   "metadata": {},
   "source": [
    "Lasso回归的目标是最小化误差平方和，同时施加所有系数的绝对值之和的惩罚。这种类型的正则化（L1正则化）可以导致系数的某些估计值精确地等于0。这意味着，Lasso回归可以有效地进行变量选择，并确定最重要的变量。L1正则化有助于处理特征数量多于样本数量的问题，防止过拟合，并且可以增强模型的预测能力。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26f7b2ac-e9c6-4d25-9593-a10da16d2a3e",
   "metadata": {},
   "source": [
    "## 二、举例说明"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65835e9c-255f-46c8-97de-eed77bcecca1",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 1、用50个方程，200个未知数来举例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66a5103a-54a4-4b1e-986c-5471b0935c9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.randn(50, 200)\n",
    "y = np.random.randn(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b5e822db-0bd0-4da8-a8f1-e574afb42b43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用普通线性回归，尝试去拟合数据\n",
    "linear = LinearRegression()\n",
    "linear.fit(x, y)\n",
    "linear.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be9cf6f9-86c1-4844-bd1b-cab08aa61af5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999473877119197"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用岭回归，尝试拟合数据\n",
    "ridge = Ridge()\n",
    "ridge.fit(x, y)\n",
    "ridge.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "15859c0f-f7ae-4a19-a921-fd769a5c789f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9999994641155945"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调整alpha岭参数\n",
    "ridge = Ridge(alpha=0.1)\n",
    "ridge.fit(x, y)\n",
    "ridge.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "007a16d0-2deb-4a5b-ba4d-1dfb8e35f82c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用Lasso回归，尝试拟合数据\n",
    "lasso = Lasso()\n",
    "lasso.fit(x, y)\n",
    "lasso.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f0584061-f6c7-44e7-8b65-bf9c8cbe671e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.761699310255391"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调整alpha惩罚系数\n",
    "lasso = Lasso(alpha=0.1)\n",
    "lasso.fit(x, y)\n",
    "lasso.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2bb746b7-101e-406c-ac5b-133e6df4b51f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.996961586676452"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lasso = Lasso(alpha=0.01)\n",
    "lasso.fit(x, y)\n",
    "lasso.score(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2cf80437-7570-4f0e-9baa-03380a7d9281",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.        , -0.00044149,  0.01640446, -0.02545892, -0.        ,\n",
       "       -0.        , -0.        , -0.        ,  0.27444887, -0.        ,\n",
       "       -0.0524476 ,  0.        ,  0.02038342,  0.0791726 , -0.        ,\n",
       "        0.        , -0.        , -0.04729488,  0.        ,  0.02369261,\n",
       "       -0.0354074 , -0.        ,  0.        , -0.        ,  0.        ,\n",
       "       -0.        , -0.        ,  0.        , -0.        ,  0.22588919,\n",
       "        0.08886908,  0.        , -0.        ,  0.        ,  0.        ,\n",
       "        0.        , -0.24599559,  0.        , -0.        , -0.        ,\n",
       "        0.13352444,  0.        , -0.        , -0.        , -0.02413642,\n",
       "       -0.        ,  0.        ,  0.        ,  0.        , -0.        ,\n",
       "        0.03859896,  0.08326463,  0.        , -0.02180867, -0.        ,\n",
       "        0.        , -0.        , -0.        ,  0.        ,  0.01820182,\n",
       "       -0.        ,  0.        ,  0.        , -0.        ,  0.        ,\n",
       "        0.15893865,  0.        ,  0.        ,  0.        ,  0.        ,\n",
       "       -0.13648884, -0.        ,  0.        ,  0.05586386,  0.03194754,\n",
       "        0.        , -0.        , -0.08409923, -0.        , -0.        ,\n",
       "        0.        ,  0.        , -0.        ,  0.        ,  0.        ,\n",
       "       -0.        ,  0.        , -0.        ,  0.        ,  0.        ,\n",
       "        0.        , -0.        , -0.        ,  0.        , -0.        ,\n",
       "       -0.        ,  0.08502179, -0.29343567,  0.10772015, -0.1618584 ,\n",
       "        0.        ,  0.0732306 ,  0.        ,  0.        ,  0.        ,\n",
       "       -0.        ,  0.        , -0.        ,  0.        , -0.00376965,\n",
       "       -0.00642144, -0.        ,  0.        , -0.14846525,  0.        ,\n",
       "        0.        , -0.        , -0.        ,  0.        ,  0.        ,\n",
       "       -0.1552701 , -0.        ,  0.        ,  0.        , -0.        ,\n",
       "       -0.        , -0.        , -0.        , -0.        , -0.00717868,\n",
       "       -0.        ,  0.        ,  0.        , -0.        ,  0.13348051,\n",
       "        0.        ,  0.        , -0.        ,  0.        , -0.        ,\n",
       "        0.        ,  0.00780144, -0.        ,  0.        ,  0.        ,\n",
       "       -0.        , -0.        ,  0.05224098,  0.        , -0.        ,\n",
       "        0.        , -0.09546032,  0.        , -0.        , -0.11467651,\n",
       "       -0.        ,  0.        , -0.        , -0.16218379, -0.        ,\n",
       "       -0.        ,  0.        , -0.        , -0.        ,  0.10542586,\n",
       "       -0.        ,  0.05191153, -0.        ,  0.17829131, -0.        ,\n",
       "       -0.        , -0.        , -0.04160273, -0.        ,  0.        ,\n",
       "        0.        ,  0.16917404, -0.        , -0.        ,  0.        ,\n",
       "        0.        , -0.        ,  0.        ,  0.        , -0.15127121,\n",
       "       -0.        , -0.05251339, -0.        ,  0.        ,  0.        ,\n",
       "        0.07380365, -0.        ,  0.        ,  0.        ,  0.04708868,\n",
       "       -0.06587462, -0.        ,  0.        , -0.        ,  0.        ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看一下Lasso回归得到的模型参数\n",
    "lasso.coef_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c12c08c-70f4-4765-9ab4-c9183e0ecf8e",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 2、用california房价数据集为例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "36e69863-108d-46ee-a061-50fae2880bc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import fetch_california_housing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "65e760b5-f736-448f-bc1c-75b4c41657b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5a9afa42-2b23-4752-9e5f-46c4588d8e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f29fab69-a1ec-4c2f-b2fe-ee9e8f598ed3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "82e53ac7-eca5-4fef-942b-8f16db71c0f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载数据集\n",
    "california = fetch_california_housing()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "81e49566-df23-4ab2-8183-59aa58c0ca6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. _california_housing_dataset:\n",
      "\n",
      "California Housing dataset\n",
      "--------------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 20640\n",
      "\n",
      "    :Number of Attributes: 8 numeric, predictive attributes and the target\n",
      "\n",
      "    :Attribute Information:\n",
      "        - MedInc        median income in block group\n",
      "        - HouseAge      median house age in block group\n",
      "        - AveRooms      average number of rooms per household\n",
      "        - AveBedrms     average number of bedrooms per household\n",
      "        - Population    block group population\n",
      "        - AveOccup      average number of household members\n",
      "        - Latitude      block group latitude\n",
      "        - Longitude     block group longitude\n",
      "\n",
      "    :Missing Attribute Values: None\n",
      "\n",
      "This dataset was obtained from the StatLib repository.\n",
      "https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html\n",
      "\n",
      "The target variable is the median house value for California districts,\n",
      "expressed in hundreds of thousands of dollars ($100,000).\n",
      "\n",
      "This dataset was derived from the 1990 U.S. census, using one row per census\n",
      "block group. A block group is the smallest geographical unit for which the U.S.\n",
      "Census Bureau publishes sample data (a block group typically has a population\n",
      "of 600 to 3,000 people).\n",
      "\n",
      "A household is a group of people residing within a home. Since the average\n",
      "number of rooms and bedrooms in this dataset are provided per household, these\n",
      "columns may take surprisingly large values for block groups with few households\n",
      "and many empty houses, such as vacation resorts.\n",
      "\n",
      "It can be downloaded/loaded using the\n",
      ":func:`sklearn.datasets.fetch_california_housing` function.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n",
      "      Statistics and Probability Letters, 33 (1997) 291-297\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(california.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f4956d2f-02c0-42fc-8161-3b81eb6beb75",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = california.data                          # 特征矩阵\n",
    "target = california.target                      # 目标变量\n",
    "feature_names = california.feature_names        # 特征名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4689dbe9-4dc8-4222-b2b1-31848216c21f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据预处理\n",
    "scaler = StandardScaler()\n",
    "data_scaled = scaler.fit_transform(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9629ccac-acda-4c63-8c22-de027c3c6676",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 划分数据集\n",
    "X_train, X_test, y_train, y_test = train_test_split(data_scaled, target, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0e7784d2-0f0d-4c58-8b8c-74beb05eeac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建Lasso模型实例，并设置正则化强度参数alpha\n",
    "lasso = Lasso(alpha=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2f7a67b0-1ace-48ff-904a-77af6b9892d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>Lasso(alpha=0.1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">Lasso</label><div class=\"sk-toggleable__content\"><pre>Lasso(alpha=0.1)</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "Lasso(alpha=0.1)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用训练数据拟合模型\n",
    "lasso.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "36a64209-2ebe-4489-aa7f-177f5a81c588",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 进行模型预测\n",
    "y_pred = lasso.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "450086df-3037-4b63-95ca-7a45cca2c129",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4776757087261867"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型评估，先计算R^2\n",
    "lasso.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2e3260d7-a58c-405d-9918-6708eab5f37c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7060372540175096"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型评估，再计算MES\n",
    "mean_squared_error(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4800184c-9ba1-47a3-92f7-e64f5a86aae1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5860088041046905"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用普通线性回归模型对比一下得分\n",
    "linear = LinearRegression()\n",
    "linear.fit(X_train, y_train)\n",
    "y_pred = linear.predict(X_test)\n",
    "linear.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "42dde5de-adb0-491b-bc0d-326098c3ee59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5596010218565981"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_squared_error(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9f5f2a78-a97f-4dbf-bc59-680db6a53369",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5883906623130962"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把alpha的值调整一下再看看效果\n",
    "lasso = Lasso(alpha=0.01)\n",
    "lasso.fit(X_train, y_train)\n",
    "y_pred = lasso.predict(X_test)\n",
    "lasso.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c68e57fa-d12f-4cde-9242-260850c3b520",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.556381411631654"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_squared_error(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06c03b09-63c0-46b7-80a9-46d0345b7c8b",
   "metadata": {},
   "source": [
    "结论：如果方程的数量（样本个数）大于未知数的个数（特征个数），Lasso回归的效果和普通线性回归几乎一样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b58f36cb-822d-4fc3-9cd0-e1356f7f1dd6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
